<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Triangle</title>
<style>
body {
	background-color: grey;
}
canvas {
	backgroud-color: white;
}
</style>
<script src="../libs/raf_polyfill.js"></script>
<script src="../libs/gl-matrix-min.js"></script>
<script id="shader-vs" type="x-shader/x-vertex">
	attribute vec3 aVertexPosition;
	attribute vec3 aVertexColor;
	
	uniform mat4 uMVMatrix;
	uniform mat4 uPMatrix;
		
	varying highp vec4 vColor;
	void main(void)
	{
		gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
		vColor = vec4(aVertexColor, 1.0);
	}
</script>
<script id="shader-fs" type="x-shader/x-fragment">
	varying highp vec4 vColor;
	void main(void){
		gl_FragColor = vColor;
	}
</script>
<script>
	var gl = null;
		canvas = null,
		glProgram = null,
		fragmentShader = null,
		vertexShader = null;
	var vertexPositionAttribute = null;
	var trianglesVerticeBuffer = null;
	var vertexColorAttribute = null;
	var trianglesColorBuffer = null;
	var angle = 0.0;
	
	var mvMatrix = mat4.create();
	var pMatrix = mat4.create();
	
	function initWebGL()
	{
		canvas = document.getElementById("my-canvas");
		try
		{
				gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
		}
		catch (e){
		}
		if (gl)
		{
			initShaders();
			setupBuffers();
			getMatrixUniforms();
			(function animLoop(){
				setupWebGL();
				setupDynamicBuffers();
				setMatrixUniforms();
				drawScene();				
				requestAnimationFrame(animLoop, canvas);
			})();

		}
		else
		{
			alert("Error: Your browser does not support WebGL");
		}
	}
	
	function setupWebGL()
	{
		gl.clearColor(0.1, 0.5, 0.1, 1.0);
		gl.clear(gl.COLOR_BUFFER_BIT);
		gl.viewport(0, 0, canvas.width, canvas.height);
		//gl.viewport(0, canvas.height/2.0, canvas.width/2.0, canvas.height/2.0);
		
		mat4.perspective(45, canvas.width / canvas.height, 0.1, 100.0, pMatrix);
		mat4.identity(mvMatrix);
		mat4.translate(mvMatrix, [0,0,-2.0]);
	}
	function getMatrixUniforms(){
		glProgram.pMatrixUniform = gl.getUniformLocation(glProgram, "uPMatrix");
		glProgram.mvMatrixUniform = gl.getUniformLocation(glProgram, "uMVMatrix");
	}
	function setMatrixUniforms(){
		gl.uniformMatrix4fv(glProgram.pMatrixUniform, false, pMatrix);
		gl.uniformMatrix4fv(glProgram.mvMatrixUniform, false, mvMatrix);
	}
	function initShaders(){
		var fs_source = document.getElementById('shader-fs').innerHTML;
		var vs_source = document.getElementById('shader-vs').innerHTML;
		
		vertexShader = makeShader(vs_source, gl.VERTEX_SHADER);
		fragmentShader = makeShader(fs_source, gl.FRAGMENT_SHADER);
		
		glProgram = gl.createProgram();
		gl.attachShader(glProgram, vertexShader);
		gl.attachShader(glProgram, fragmentShader);
		gl.linkProgram(glProgram);
		
		if (!gl.getProgramParameter(glProgram, gl.LINK_STATUS)){
			alert('unable to initialize the shader program.');
		}
		gl.useProgram(glProgram);
	}
	function makeShader(src, type){
		var shader = gl.createShader(type);
		gl.shaderSource(shader, src);
		gl.compileShader(shader);
		if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)){
			alert('Error compiling shader: ' + gl.getShaderInfoLog(shader));
		}
		return shader;
	}
	function setupBuffers(){
		var triangleVerticeColors = [
			1.0,0.0,0.0,
			1.0,1.0,1.0,
			1.0,0.0,0.0,
			
			0.0,0.0,1.0,
			1.0,1.0,1.0,
			0.0,0.0,1.0
		];
		trianglesColorBuffer = gl.createBuffer();
		gl.bindBuffer(gl.ARRAY_BUFFER, trianglesColorBuffer);
		gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(triangleVerticeColors), gl.STATIC_DRAW);
	}
	function setupDynamicBuffers()
	{
		var x_translation = Math.sin(angle)/2.0;
		var triangleVertices = [
			-0.5 + x_translation, 0.5, -0.5,
			 0.0 + x_translation, 0.0, -0.5,
			-0.5 + x_translation,-0.5, -0.5,
			
			 0.5 + x_translation, 0.5, 0.5,
			 0.0 + x_translation, 0.0, 0.5,
			 0.5 + x_translation,-0.5, 0.5 
		];
		angle += 0.01;
		
		trianglesVerticeBuffer = gl.createBuffer();
		gl.bindBuffer(gl.ARRAY_BUFFER, trianglesVerticeBuffer);
		gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(triangleVertices), gl.DYNAMIC_DRAW);
	}
	
	function drawScene(){
		vertexPositionAttribute = gl.getAttribLocation(glProgram, "aVertexPosition");
		gl.enableVertexAttribArray(vertexPositionAttribute);
		gl.bindBuffer(gl.ARRAY_BUFFER, trianglesVerticeBuffer);
		gl.vertexAttribPointer(vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0);
		
		vertexColorAttribute = gl.getAttribLocation(glProgram, "aVertexColor");
		gl.enableVertexAttribArray(vertexColorAttribute);
		gl.bindBuffer(gl.ARRAY_BUFFER, trianglesColorBuffer);
		gl.vertexAttribPointer(vertexColorAttribute, 3, gl.FLOAT, false, 0, 0);
		
		gl.drawArrays(gl.TRIANGLES, 0, 6);
	}
</script>
</head>
<body onload="initWebGL()">
<canvas id="my-canvas" width="400" height="300"> Your browser does not support the HTML5 canvas element </canvas>
</body>

<body>
</body>
</html>
